package com.freecloud.javabasics.design.single;

import com.freecloud.javabasics.design.entity.car.BMW;
import com.freecloud.javabasics.design.entity.car.Car;

/**
 * 单例：双重校验锁单例实现
 * @Author: maomao
 * @Date: 2020-02-29 21:34
 */
public class DoubleChecked {

    private static Car car = null;

    public static Car getInstance(){
        if(car != null){
            return car;
        }
        //通过代码锁实现安全创建对象
        synchronized (Car.class){
            if(car == null){
                car = new BMW();
            }
            return car;
        }
    }
}
